#!/usr/bin/ruby

require 'drb/drb'

READ_BUF_SIZE = 1024

class Clt
  attr_reader :name,:w
  
  def initialize(name)
    @name = name
    @r,@w = IO.pipe
  end
  
  def connect(svr)
    @svr_w = svr.bind(self)
  end
  
  def puts(msg)
    @svr_w.write(msg)
  end
  
  def gets
    @r.read_nonblock READ_BUF_SIZE
  rescue IO::EAGAINWaitReadable =>e
    nil
    #retry
  end
  
  def to_s
    "#{@name}"
  end
end

uri = "druby://localhost:8087"

DRb.start_service

svr = DRbObject.new_with_uri(uri)
puts "username : #{ARGV[0]}"
clt = Clt.new(ARGV[0].to_s)
#clt = Clt.new("XXX")
clt.connect svr

loop do
  until str = clt.gets
    
  end
  puts str
  break if str =~ /bye/i
  clt.puts STDIN.gets
end
#puts svr.get_current_time
